# -*- coding: UTF-8 -*-
# 作者： 磊
# @Time：2022/5/2
import os
import time

import pytest

# if __name__ == '__main__':
#     # pytest.main(['-vs','Demo01/'])
#     # pytest.main(['-vs', '--html=report/report.html'])
#     # pytest.main(['-vs', '--maxfail=1']) #出现N个失败就终止测试。
#     pytest.main()
#     time.sleep(3)
#     os.system("allure generate ./temp -o ./report --clean")




import os
import traceback
import pytest
# from utils import project_name
# from utils.logUtils.logControl import INFO
# from utils import get_notification_type
# from utils.noticUtils.weChatSendControl import WeChatSend
# from utils.noticUtils.dingtalkControl import DingTalkSendMsg
# from utils.noticUtils.sendmailControl import SendEmail
# from Enums.notificationType_enum import NotificationType
# from utils.noticUtils.feishuControl import FeiShuTalkChatBot
# from utils.readFilesUtils.caseAutomaticControl import TestCaseAutomaticGeneration

from Demo08.Enums.notificationType_enum import NotificationType
from Demo08.utils import project_name, get_notification_type
from Demo08.utils.logUtils.logControl import INFO
from Demo08.utils.noticUtils.dingtalkControl import DingTalkSendMsg
from Demo08.utils.noticUtils.feishuControl import FeiShuTalkChatBot
from Demo08.utils.noticUtils.sendmailControl import SendEmail
from Demo08.utils.noticUtils.weChatSendControl import WeChatSend


def run():
    # 从配置文件中获取项目名称
    try:
        INFO.logger.info(
            """
                      .::::.
                    .::::::::.
                   :::::::::::
                ..:::::::::::'
             '::::::::::::'
               .::::::::::
          '::::::::::::::..
               ..::::::::::::.
             ``::::::::::::::::
              ::::``:::::::::'        .:::.
             ::::'   ':::::'       .::::::::.
           .::::'      ::::     .:::::::'::::.
          .:::'       :::::  .:::::::::' ':::::.
         .::'        :::::.:::::::::'      ':::::.
        .::'         ::::::::::::::'         ``::::.
    ...:::           ::::::::::::'              ``::.
   ````':.          ':::::::::'                  ::::..
                      '.:::::'                    ':'````..
          美女坐镇                 BUG无存
————————————————————————————————————————————————————————————————
                  开始执行{}项目...
                """.format(project_name)
        )
        # 判断现有的测试用例，如果未生成测试代码，则自动生成
        # TestCaseAutomaticGeneration().get_case_automatic()

        pytest.main(['-s', '-W', 'ignore:Module already imported:pytest.PytestWarning',
                     '--alluredir', './Demo08/report/tmp', "--clean-alluredir"])
        """
                   --reruns: 失败重跑次数
                   --count: 重复执行次数
                   -v: 显示错误位置以及错误的详细信息
                   -s: 等价于 pytest --capture=no 可以捕获print函数的输出
                   -q: 简化输出信息
                   -m: 运行指定标签的测试用例
                   -x: 一旦错误，则停止运行
                   --maxfail: 设置最大失败次数，当超出这个阈值时，则不会在执行测试用例
                    "--reruns=3", "--reruns-delay=2"
                   """
        #根据tmp的零时文件，生成html报告
        os.system("allure generate ./Demo08/report/tmp -o ./Demo08/report/html --clean")

        # 判断通知类型，根据配置发送不同的报告通知
        if get_notification_type() == NotificationType.DEFAULT.value:
            pass
        elif get_notification_type() == NotificationType.DING_TALK.value:
            DingTalkSendMsg().send_ding_notification()
        elif get_notification_type() == NotificationType.WECHAT.value:
            WeChatSend().send_wechat_notification()
        elif get_notification_type() == NotificationType.EMAIL.value:
            SendEmail().send_main()
        elif get_notification_type() == NotificationType.FEI_SHU.value:
            FeiShuTalkChatBot().post()
        else:
            raise ValueError("通知类型配置错误，暂不支持该类型通知")
        # os.system(f"allure serve ./report/temp -p 9999")

    except Exception:
        # 如有异常，相关异常发送邮件
        e = traceback.format_exc()
        send_email = SendEmail()
        send_email.error_mail(e)
        raise
if __name__ == '__main__':
    run()